﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using WorldBankBBS.Security;

namespace WorldBankBBS.Data
{
    public partial class User : IUser
    {
        public static User LoadByUsername(string username, string password)
        {
            using (var entities = new WorldBankBBSEntities())
            {
                var user = entities.Users.FirstOrDefault(x => x.UserName == username);

                if (user != null)
                {
                    var prefs = user.UserPreference;
                    var p = MD5.Create().ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));

                    int i = 0;
                    for(; i < 16; ++i)
                    {
                        if (p[i] != user.Password[i]) break;
                    }

                    if (i == 16) return user;
                }
            }

            return null;
        }

        public static bool UserExists(string username)
        {
            using (var entities = new WorldBankBBSEntities())
            {
                var user = entities.Users.FirstOrDefault(x => x.UserName == username);

                return user != null;
            }
        }
        public class UserExistsException : ApplicationException
        {
            public string Username { get; private set; }
            public UserExistsException(string username)
            {
                Username = username;
            }
        }

        public static User CreateAndAddUser(string username, string password, string emailAddress, string alias,
            string realName, string website)
        {
            using (var entities = new WorldBankBBSEntities())
            {
                var user = entities.Users.FirstOrDefault(x => x.UserName == username);

                if (user != null)
                {
                    throw new UserExistsException(username);
                }

                user = new User
                {
                    Alias = alias,
                    EMailAddress = emailAddress,
                    Password = MD5.Create().ComputeHash(ASCIIEncoding.ASCII.GetBytes(password)),
                    RealName = realName,
                    UserName = username,
                    UserUID = Guid.NewGuid(),
                    Website = website
                };

                entities.Users.Add(user);

                entities.SaveChanges();

                return user;
            }

            return null;
        }

        public bool IsLoggedIn { get; set; }

        public static User LoadByAlias(string alias)
        {
            using (var entities = new WorldBankBBSEntities())
            {
                var user = entities.Users.FirstOrDefault(x => x.Alias == alias);

                return user;
            }
        }

        public static User LoadByUsername(string username)
        {
            using (var entities = new WorldBankBBSEntities())
            {
                var user = entities.Users.FirstOrDefault(x => x.UserName == username);

                return user;
            }
        }
    }
}
